package com.xlcszsgc.module.blob.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.xlcszsgc.core.basedao.base.impl.BaseDao;
import com.xlcszsgc.core.util.PropertyUtil;
import com.xlcszsgc.module.blob.dao.IBlobDao;
import com.xlcszsgc.module.blob.domain.BaseBlob;
import com.xlcszsgc.module.blob.ui.commond.BaseBlobCommond;

/**
 * @Title: BlobDaoImpl.java
 * @Package com.xlcszsgc.module.blob.dao.impl
 * @Description: dao层支持
 * @author Zhaoqt  
 * @date 2014-5-23 上午12:36:04
 * @version V1.0
 */
@Repository(value = "com.xlcszsgc.module.blob.dao.impl.BlobDaoImpl")
public class BlobDaoImpl extends BaseDao implements IBlobDao {

	@Override
	public void addBlob(BaseBlob baseBlob) throws Exception {
		this.addEntity(baseBlob);
	}

	@Override
	public void deleteBlobById(BaseBlob baseBlob) throws Exception {
		this.deleteEntityByPK(baseBlob);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BaseBlob> listBlob(BaseBlobCommond baseBlobCommond,
			int firstResult, int maxResult) throws Exception {
		if(PropertyUtil.objectNotEmpty(baseBlobCommond.getQueryString())) {
			Session session = this.getHibernateSession();
			Map<String, Object> paramMap = baseBlobCommond.getParamMap();
			Query query = session.createQuery(
					baseBlobCommond.getQueryString());
			if ((paramMap != null) && (paramMap.size() > 0)) {
		          query.setProperties(paramMap);
		    }
			if (firstResult > 0) {
				query.setFirstResult(firstResult);
	        }
	        if (maxResult > 0) {
	        	query.setMaxResults(maxResult);
	        }
	        query.setResultTransformer(Transformers.aliasToBean(BaseBlob.class));  
	        baseBlobCommond.setResultCollection(query.list());
			return (List<BaseBlob>)baseBlobCommond.getResultCollection();
		} 
		return null;
	}

	@Override
	public Long countBlob(BaseBlobCommond baseBlobCommond) throws Exception {
		if(PropertyUtil.objectNotEmpty(baseBlobCommond.getQueryString())) {
			Session session = this.getHibernateSession();
			Map<String, Object> paramMap = baseBlobCommond.getParamMap();
				int startIndex = -1;
		    	String countHQL = baseBlobCommond.getQueryString(); 
		    	countHQL = countHQL.substring(countHQL.toUpperCase().indexOf("FROM"), countHQL.length());
		        
		    	if ((startIndex = countHQL.toUpperCase().indexOf("ORDER BY")) != -1) {
		        	countHQL = countHQL.substring(0, startIndex);
		        } 
		        
		        Query query = null;
		        query = session.createQuery("SELECT COUNT(*) " + countHQL );
				if ((paramMap != null) && (paramMap.size() > 0)) {
			          query.setProperties(paramMap);
			    }
				return (Long)query.list().iterator().next();
		} 
		return Long.valueOf(0);
	}

	@Override
	public BaseBlob findObject(BaseBlobCommond baseBlobCommond)
			throws Exception {
		return (BaseBlob)findObject(BaseBlob.class, baseBlobCommond.getSearchBlobId());
	}

	@Override
	public Long getCountBaseBlob(BaseBlobCommond baseBlobCommond)
			throws Exception {
		String hql=" select count(*) from BaseBlob b where b.attName ='smallFile' and b.categoryCode='zjzs' ";
		Session session = this.getHibernateSession();
		Query query = null;
        query = session.createQuery(hql);
		
        return (Long)query.list().iterator().next();
	}

	@Override
	public void getResBaseBlob(BaseBlobCommond baseBlobCommond)
			throws Exception {
		
		String hql=" select b.blobId as blobId,b.baseBlob as baseBlob from BaseBlob b where b.categoryCode='zjzs' and b.attName ='smallFile' order by b.sortNum,b.blobId ";
		
		Session session = this.getHibernateSession();
		Query query = null;
        query = session.createQuery(hql);
        if (baseBlobCommond.getPageBean().getFirstResult() > 0) {
			query.setFirstResult(baseBlobCommond.getPageBean().getFirstResult());
        }
        if (baseBlobCommond.getPageBean().getMaxResult() > 0) {
        	query.setMaxResults(baseBlobCommond.getPageBean().getMaxResult());
        }
        query.setResultTransformer(Transformers.aliasToBean(BaseBlob.class));
        baseBlobCommond.setResultCollection(query.list());
	}

	@Override
	public List<BaseBlob> listBigBlogForDel(String ids) throws Exception {
		List<BaseBlob> blobList=new ArrayList<BaseBlob>();
		String sql="  from BaseBlob b where b.blobId in ("+ids+") ";
		Session session = this.getHibernateSession();
		
		Query query =session.createQuery(sql);
		
		blobList=(List<BaseBlob>)query.list();
		
		return blobList;
	}
	
}
